#include "StdAfx.h"
#include "NetMsgListener.h"
#include <fgeMessageManager.h>

using namespace fge;
using namespace protocol;
u_long	WINAPI SendToClient( const char* body, u_long size, ConnectionID id)
{
	return Net::s_pNetGameServer->Send(body,size,id);
}
u_long	WINAPI SendToClient( u_long msgid, const char* body, u_long size, ConnectionID id)
{
	protocol::NetMsg head;
	head.msg_id = msgid;
	head.msg_len = size;
	head.msg_num = 1;
	ocl::Packer	msg;
	msg.AddToBuf(&head,sizeof(head));
	msg.AddToBuf(body,size);
	return SendToClient(msg(),msg.GetBufferLen(),id);
}
u_long	WINAPI SendToDB( const char* body, u_long size )
{
	//CMessageManager* pMsgQueue = INSTANCE(CGameServer)->GetMessageQueue();
	NetMsg *pMsg = (NetMsg*)body;
	INSTANCE(CDBServer)->Push(pMsg->msg_id,(BYTE*)pMsg,sizeof(NetMsg),(BYTE*)pMsg+sizeof(NetMsg),size-sizeof(NetMsg));
	//return Net::s_pNetDB->Send(body,size);
	return size;
}
u_long	WINAPI SendToDB( u_long msgid, const void* body, u_long size)
{
	protocol::NetMsg head;
	head.msg_id = msgid;
	head.msg_len = size;
	head.msg_num = 1;
	ocl::Packer	msg;
	msg.AddToBuf(&head,sizeof(head));
	msg.AddToBuf(body,size);
	return SendToDB(msg(),msg.GetBufferLen());
}
//int WINAPI SendServiceMsg(  DWORD serviceId,DWORD msg, WPARAM wParam, LPARAM lParam )
//{
//	return 0;
//}
CNetMsgListener::CNetMsgListener(void)
{
}

CNetMsgListener::~CNetMsgListener(void)
{
}

void	CNetMsgListener::OnAccept( const char* ip, int port, ConnectionID id )
{
	//m_msgBuf[id.nID].clear();
}
void	CNetMsgListener::OnDisconnect( ConnectionID id )
{
	//MsgBuf::iterator i=m_msgBuf.find(id.nID);
	//if( i==m_msgBuf.end())
	//	return;
	//m_msgBuf.erase(i);
}
void	CNetMsgListener::OnSend( const char* body, u_long size, ConnectionID id )
{

}
void	CNetMsgListener::OnRecv( const char* body, u_long size, ConnectionID id )
{
	if( size<=0 )
		return;

	//MsgBuf::iterator i=m_msgBuf.find(id.nID);
	//if( i==m_msgBuf.end() )
	//	return;

	CMessageManager* pMsgQueue = INSTANCE(CGameServer)->GetMessageQueue();
	NetMsg *pMsg = (NetMsg*)body;
	if(pMsg->msg_len == size)
		pMsgQueue->Push(pMsg->msg_id,(BYTE*)pMsg,sizeof(NetMsg),(BYTE*)pMsg+sizeof(NetMsg),size-sizeof(NetMsg));
}
void	CNetMsgListener::OnClose( )
{

}
